<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struct template placeholder</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../xpressive/reference.html#doxygen.xpressive__fwd_8hpp" title="Header &lt;boost/xpressive/xpressive_fwd.hpp&gt;">
<link rel="prev" href="structboost_1_1xpressive_1_1null__regex__traits.html" title="Struct template null_regex_traits">
<link rel="next" href="structboost_1_1xpressive_1_1reference.html" title="Struct template reference">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="structboost_1_1xpressive_1_1null__regex__traits.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../xpressive/reference.html#doxygen.xpressive__fwd_8hpp"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="structboost_1_1xpressive_1_1reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="refentry">
<a name="doxygen.structboost_1_1xpressive_1_1placeholder"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2><span class="refentrytitle">Struct template placeholder</span></h2>
<p>boost::xpressive::placeholder — For defining a placeholder to stand in for a variable a semantic action. </p>
</div>
<h2 class="refsynopsisdiv-title">Synopsis</h2>
<div class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: &lt;<a class="link" href="../xpressive/reference.html#doxygen.xpressive__fwd_8hpp" title="Header &lt;boost/xpressive/xpressive_fwd.hpp&gt;">boost/xpressive/xpressive_fwd.hpp</a>&gt;

</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="keyword">int</span> I <span class="special">=</span> <span class="number">0</span><span class="special">&gt;</span> 
<span class="keyword">struct</span> <a class="link" href="structboost_1_1xpressive_1_1placeholder.html" title="Struct template placeholder">placeholder</a> <span class="special">{</span>

  <span class="comment">// <a class="link" href="structboost_1_1xpressive_1_1placeholder.html#id-1_3_44_5_18_2_1_9_4-bb">public member functions</a></span>
  <span class="emphasis"><em><span class="identifier">unspecified</span></em></span> <a class="link" href="structboost_1_1xpressive_1_1placeholder.html#doxygen.structboost_1_1xpressive_1_1placeholder_1ad172509c2232338ab291a164c7df40b7"><span class="keyword">operator</span><span class="special">=</span></a><span class="special">(</span><span class="identifier">T</span> <span class="special">&amp;</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
  <span class="emphasis"><em><span class="identifier">unspecified</span></em></span> <a class="link" href="structboost_1_1xpressive_1_1placeholder.html#doxygen.structboost_1_1xpressive_1_1placeholder_1aadeff1977e9a2e8e63b227bacdcd9b7a"><span class="keyword">operator</span><span class="special">=</span></a><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span> <span class="special">&amp;</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
<span class="special">}</span><span class="special">;</span></pre></div>
<div class="refsect1">
<a name="id-1.3.44.5.19.19.4"></a><h2>Description</h2>
<p>Use <code class="computeroutput">placeholder&lt;&gt;</code> to define a placeholder for use in semantic actions to stand in for real objects. The use of placeholders allows regular expressions with actions to be defined once and reused in many contexts to read and write from objects which were not available when the regex was defined.</p>
<p>
You can use <code class="computeroutput">placeholder&lt;&gt;</code> by creating an object of type <code class="computeroutput">placeholder&lt;T&gt;</code> and using that object in a semantic action exactly as you intend an object of type <code class="computeroutput">T</code> to be used. </p>
<pre class="programlisting"><span class="identifier">placeholder</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">_i</span><span class="special">;</span>
<span class="identifier">placeholder</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <a class="link" href="namespaceboost_1_1xpressive_1afae3ed7c3a8ebd3c2f06a943ff974a58.html" title="Global _d"><span class="identifier">_d</span></a><span class="special">;</span>

<a class="link" href="../xpressive/reference.html#doxygen.namespaceboost_1_1xpressive_1ad82b5700d5ba5eb195cfce6d4007f0b4"><span class="identifier">sregex</span></a> <span class="identifier">rex</span> <span class="special">=</span> <span class="special">(</span> <span class="identifier">some</span> <span class="special">&gt;&gt;</span> <span class="identifier">regex</span> <span class="special">&gt;&gt;</span> <span class="identifier">here</span> <span class="special">)</span>
    <span class="special">[</span> <span class="special">++</span><span class="identifier">_i</span><span class="special">,</span> <a class="link" href="namespaceboost_1_1xpressive_1afae3ed7c3a8ebd3c2f06a943ff974a58.html" title="Global _d"><span class="identifier">_d</span></a> <span class="special">*=</span> <a class="link" href="namespaceboost_1_1xpressive_1afae3ed7c3a8ebd3c2f06a943ff974a58.html" title="Global _d"><span class="identifier">_d</span></a> <span class="special">]</span><span class="special">;</span>
</pre>
<p> Then, when doing a pattern match with either <code class="computeroutput"><a class="link" href="namespaceboost_1_1xpressive_1a1194c854fb35ddb2d0b216a303263bb3.html" title="Function regex_search">regex_search()</a></code>, <code class="computeroutput"><a class="link" href="namespaceboost_1_1xpressive_1a7549c8bcd5ce2471d42aaad4e409d148.html" title="Function regex_match">regex_match()</a></code> or <code class="computeroutput"><a class="link" href="namespaceboost_1_1xpressive_1a53c66a9ef9dc507a2a89b06d4822aac2.html" title="Function regex_replace">regex_replace()</a></code>, pass a <code class="computeroutput"><a class="link" href="structboost_1_1xpressive_1_1match__results.html" title="Struct template match_results">match_results</a>&lt;&gt;</code> object that contains bindings for the placeholders used in the regex object's semantic actions. You can create the bindings by calling <code class="computeroutput"><a class="link" href="structboost_1_1xpressive_1_1match__results.html#doxygen.structboost_1_1xpressive_1_1match__results_1a0b5d9b054f18782e2e195dab5e39123a">match_results::let</a></code> as follows: </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
<span class="keyword">double</span> <span class="identifier">d</span> <span class="special">=</span> <span class="number">3</span><span class="special">.</span><span class="number">14</span><span class="special">;</span>

<a class="link" href="../xpressive/reference.html#doxygen.namespaceboost_1_1xpressive_1a9abbae50995ee1162f681ed99477c0ed"><span class="identifier">smatch</span></a> <span class="identifier">what</span><span class="special">;</span>
<span class="identifier">what</span><span class="special">.</span><span class="identifier">let</span><span class="special">(</span><span class="identifier">_i</span> <span class="special">=</span> <span class="identifier">i</span><span class="special">)</span>
    <span class="special">.</span><span class="identifier">let</span><span class="special">(</span><a class="link" href="namespaceboost_1_1xpressive_1afae3ed7c3a8ebd3c2f06a943ff974a58.html" title="Global _d"><span class="identifier">_d</span></a> <span class="special">=</span> <span class="identifier">d</span><span class="special">)</span><span class="special">;</span>

<span class="keyword">if</span><span class="special">(</span><a class="link" href="namespaceboost_1_1xpressive_1a7549c8bcd5ce2471d42aaad4e409d148.html" title="Function regex_match"><span class="identifier">regex_match</span></a><span class="special">(</span><span class="string">"some string"</span><span class="special">,</span> <span class="identifier">rex</span><span class="special">,</span> <span class="identifier">what</span><span class="special">)</span><span class="special">)</span>
   <span class="comment">// i and d mutated here</span>
</pre>
<p> If a semantic action executes that contains an unbound placeholder, a exception of type <code class="computeroutput"><a class="link" href="structboost_1_1xpressive_1_1regex__error.html" title="Struct regex_error">regex_error</a></code> is thrown.</p>
<p>See the discussion for <code class="computeroutput"><a class="link" href="namespaceboost_1_1xpressive_1ace777d41d9a728658b3569d818e70d52.html" title="Function template let">xpressive::let()</a></code> and the  <a class="link" href="../xpressive/user_s_guide.html#boost_xpressive.user_s_guide.semantic_actions_and_user_defined_assertions.referring_to_non_local_variables"> "Referring to Non-Local Variables"</a> section in the Users' Guide for more information.</p>
<p><span class="emphasis"><em>Example:</em></span> </p>
<pre class="programlisting"><span class="comment">// Define a placeholder for a map object:</span>
<span class="identifier">placeholder</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">_map</span><span class="special">;</span>

<span class="comment">// Match a word and an integer, separated by =&gt;,</span>
<span class="comment">// and then stuff the result into a std::map&lt;&gt;</span>
<a class="link" href="../xpressive/reference.html#doxygen.namespaceboost_1_1xpressive_1ad82b5700d5ba5eb195cfce6d4007f0b4"><span class="identifier">sregex</span></a> <span class="identifier">pair</span> <span class="special">=</span> <span class="special">(</span> <span class="special">(</span><a class="link" href="namespaceboost_1_1xpressive_1ad80f6f2e945d16adb777f6c5c05c32ba.html" title="Global s1"><span class="identifier">s1</span></a><span class="special">=</span> <span class="special">+</span><a class="link" href="namespaceboost_1_1xpressive_1a6d4422b6535a447d3eb0689827b0b8fe.html" title="Global _w"><span class="identifier">_w</span></a><span class="special">)</span> <span class="special">&gt;&gt;</span> <span class="string">"=&gt;"</span> <span class="special">&gt;&gt;</span> <span class="special">(</span><a class="link" href="namespaceboost_1_1xpressive_1a1f240bb30623f86f70e07f05bbd7b086.html" title="Global s2"><span class="identifier">s2</span></a><span class="special">=</span> <span class="special">+</span><a class="link" href="namespaceboost_1_1xpressive_1afae3ed7c3a8ebd3c2f06a943ff974a58.html" title="Global _d"><span class="identifier">_d</span></a><span class="special">)</span> <span class="special">)</span>
    <span class="special">[</span> <span class="identifier">_map</span><span class="special">[</span><a class="link" href="namespaceboost_1_1xpressive_1ad80f6f2e945d16adb777f6c5c05c32ba.html" title="Global s1"><span class="identifier">s1</span></a><span class="special">]</span> <span class="special">=</span> <a class="link" href="namespaceboost_1_1xpressive_1a149e8cb8ad542bdabd5c1980bb584c21.html" title="Function template as"><span class="identifier">as</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></a><span class="special">(</span><a class="link" href="namespaceboost_1_1xpressive_1a1f240bb30623f86f70e07f05bbd7b086.html" title="Global s2"><span class="identifier">s2</span></a><span class="special">)</span> <span class="special">]</span><span class="special">;</span>

<span class="comment">// Match one or more word/integer pairs, separated</span>
<span class="comment">// by whitespace.</span>
<a class="link" href="../xpressive/reference.html#doxygen.namespaceboost_1_1xpressive_1ad82b5700d5ba5eb195cfce6d4007f0b4"><span class="identifier">sregex</span></a> <span class="identifier">rx</span> <span class="special">=</span> <span class="identifier">pair</span> <span class="special">&gt;&gt;</span> <span class="special">*</span><span class="special">(</span><span class="special">+</span><a class="link" href="namespaceboost_1_1xpressive_1ab34b82560c0e1148b5c1748351711294.html" title="Global _s"><span class="identifier">_s</span></a> <span class="special">&gt;&gt;</span> <span class="identifier">pair</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// The string to parse</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <a class="link" href="namespaceboost_1_1xpressive_1a3359fafa9e5c7964a0bd5364de930e9c.html" title="Global str"><span class="identifier">str</span></a><span class="special">(</span><span class="string">"aaa=&gt;1 bbb=&gt;23 ccc=&gt;456"</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// Here is the actual map to fill in:</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">result</span><span class="special">;</span>

<span class="comment">// Bind the _map placeholder to the actual map</span>
<a class="link" href="../xpressive/reference.html#doxygen.namespaceboost_1_1xpressive_1a9abbae50995ee1162f681ed99477c0ed"><span class="identifier">smatch</span></a> <span class="identifier">what</span><span class="special">;</span>
<span class="identifier">what</span><span class="special">.</span><span class="identifier">let</span><span class="special">(</span> <span class="identifier">_map</span> <span class="special">=</span> <span class="identifier">result</span> <span class="special">)</span><span class="special">;</span>

<span class="comment">// Execute the match and fill in result map</span>
<span class="keyword">if</span><span class="special">(</span><a class="link" href="namespaceboost_1_1xpressive_1a7549c8bcd5ce2471d42aaad4e409d148.html" title="Function regex_match"><span class="identifier">regex_match</span></a><span class="special">(</span><a class="link" href="namespaceboost_1_1xpressive_1a3359fafa9e5c7964a0bd5364de930e9c.html" title="Global str"><span class="identifier">str</span></a><span class="special">,</span> <span class="identifier">what</span><span class="special">,</span> <span class="identifier">rx</span><span class="special">)</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span><span class="special">[</span><span class="string">"aaa"</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span><span class="special">[</span><span class="string">"bbb"</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span><span class="special">[</span><span class="string">"ccc"</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p> </p>
<div class="refsect2">
<a name="id-1.3.44.5.19.19.4.6"></a><h3>Template Parameters</h3>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<pre class="literallayout"><span class="keyword">typename</span> T</pre>
<p>The type of the object for which this placeholder stands in. </p>
</li>
<li class="listitem">
<pre class="literallayout"><span class="keyword">int</span> I <span class="special">=</span> <span class="number">0</span></pre>
<p>An optional identifier that can be used to distinguish this placeholder from others that may be used in the same semantic action that happen to have the same type.</p>
</li>
</ol></div>
</div>
<div class="refsect2">
<a name="id-1.3.44.5.19.19.4.7"></a><h3>
<a name="id-1_3_44_5_18_2_1_9_4-bb"></a><code class="computeroutput">placeholder</code> public member functions</h3>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<pre class="literallayout"><span class="emphasis"><em><span class="identifier">unspecified</span></em></span> <a name="doxygen.structboost_1_1xpressive_1_1placeholder_1ad172509c2232338ab291a164c7df40b7"></a><span class="keyword">operator</span><span class="special">=</span><span class="special">(</span><span class="identifier">T</span> <span class="special">&amp;</span> t<span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre>
<p>

</p>
<div class="variablelist"><table border="0" class="variablelist compact">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term">Parameters:</span></p></td>
<td><div class="variablelist"><table border="0" class="variablelist compact">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><code class="computeroutput">t</code></span></p></td>
<td><p>The object to associate with this placeholder </p></td>
</tr></tbody>
</table></div></td>
</tr>
<tr>
<td><p><span class="term">Returns:</span></p></td>
<td><p>An object of unspecified type that records the association of <code class="computeroutput">t</code> with <code class="computeroutput">*this</code>. </p></td>
</tr>
</tbody>
</table></div>
</li>
<li class="listitem">
<pre class="literallayout"><span class="emphasis"><em><span class="identifier">unspecified</span></em></span> <a name="doxygen.structboost_1_1xpressive_1_1placeholder_1aadeff1977e9a2e8e63b227bacdcd9b7a"></a><span class="keyword">operator</span><span class="special">=</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span> <span class="special">&amp;</span> t<span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre>This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. </li>
</ol></div>
</div>
</div>
</div>
<div class="copyright-footer">Copyright © 2007 Eric Niebler<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="structboost_1_1xpressive_1_1null__regex__traits.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../xpressive/reference.html#doxygen.xpressive__fwd_8hpp"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="structboost_1_1xpressive_1_1reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
